import Cookies from 'js-cookie'

const TokenKey = 'sport_admin_token'
const RefreshTokenKey = 'sport_admin_refresh_token'
const TokenType = 'sport_token_type'
const TokenScope = 'sport_token_scope'

export function setTokenInfo(tokenInfo) {
  setToken(tokenInfo.access_token, tokenInfo.expires_in)
  setRefreshToken(tokenInfo.refresh_token)
  setTokenType(tokenInfo.token_type)
  setTokenScope(tokenInfo.scope)
}

export function removeTokenInfo() {
  removeToken()
  removeRefreshToken()
  removeTokenType()
  removeTokenScope()
}

export function getToken() {
  const token = Cookies.get(TokenKey)
  console.log('cookie get token', token)
  return token
}

export function getRefreshToken() {
  return Cookies.get(RefreshTokenKey)
}

export function getTokenType() {
  return Cookies.get(TokenType)
}

export function getTokenScope() {
  return Cookies.get(TokenScope)
}

export function setToken(token, expiresSecond) {
  const expires = new Date()
  expires.setTime(expires.getTime() + expiresSecond * 1000 - 60000)
  return Cookies.set(TokenKey, token, {
    expires: expires
  })
}

export function setRefreshToken(refreshToken) {
  const expires = new Date()
  expires.setTime(expires.getTime() + 1200 * 1000 - 10 * 1000)
  return Cookies.set(RefreshTokenKey, refreshToken, {
    expires: expires
  })
}

export function setTokenType(tokenType) {
  return Cookies.set(TokenType, tokenType)
}

export function setTokenScope(tokenScope) {
  return Cookies.set(TokenScope, tokenScope)
}

export function removeToken() {
  return Cookies.remove(TokenKey)
}

export function removeRefreshToken() {
  return Cookies.remove(RefreshTokenKey)
}

export function removeTokenType() {
  return Cookies.remove(TokenType)
}

export function removeTokenScope() {
  return Cookies.remove(TokenScope)
}
